package com.itestor.iptv.modules.ne.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.itestor.iptv.global.GlobalConstant;
import com.itestor.iptv.global.exception.WebApplicationException;
import com.itestor.iptv.global.utils.LoggerUtils;
import com.itestor.iptv.modules.common.service.RegionManageService;
import com.itestor.iptv.modules.common.vo.RegionManageOBJ;
import com.itestor.iptv.modules.ne.service.RobotManageService;
import com.itestor.iptv.modules.ne.utils.NeConstant;
import com.itestor.iptv.modules.ne.vo.RobotCFGOBJ;
import com.itestor.iptv.modules.ne.vo.RobotFileInfoOBJ;
import com.itestor.iptv.modules.ne.vo.RobotManageOBJ;
import com.itestor.iptv.modules.ne.vo.RobotNetInfoOBJ;

/**
 * 
 * @author Mr.li
 * 
 */
public class RobotManageModifyAction extends DispatchAction {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger
            .getLogger(RobotManageModifyAction.class);

    /**
     * 业务对象
     */
    private RobotManageService robotManageService;

    /**
     * 
     */
    private RegionManageService regionManageService;

    /**
     * @param mapping struts mapping
     * @param request request
     * @param form action form
     * @param response response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward modifyRobot(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String idRobot =request.getParameter("idRobot");
        String name = request.getParameter("name");
        String channel = request.getParameter("channel");        
        String region = request.getParameter("region");
        String subregion = request.getParameter("subregion");

               
        if (StringUtils.isEmpty(name)) {
            throw new WebApplicationException("global.error.submit",
                    GlobalConstant.PROPERTIES_BUNDLE);
        }
        if (StringUtils.isEmpty(idRobot)){
            throw new WebApplicationException("global.error.submit",
                    GlobalConstant.PROPERTIES_BUNDLE);
        }
        
        RobotManageOBJ bean = robotManageService.get(Integer.parseInt(idRobot)); 

        bean.setChannel(Integer.parseInt(channel));
        if (!StringUtils.isEmpty(subregion)) {
            RegionManageOBJ regionManageOBJ=regionManageService.get(Integer.parseInt(subregion));
            bean.setRegionManageOBJ(regionManageOBJ);
        } else if(!StringUtils.isEmpty(region)){
            RegionManageOBJ regionManageOBJ=regionManageService.get(Integer.parseInt(region));
            bean.setRegionManageOBJ(regionManageOBJ);
        }
        
        if (bean != null) {
            request.setAttribute(NeConstant.REQUEST_ATTR_ROBOT, bean);
            
            LoggerUtils.logActionInfo("Modify the RegionManage - regionName: "
                    + name, logger, request);
            
            robotManageService.modify(bean);
        }
        
        return mapping.findForward(GlobalConstant.FORWARD_SUCCESS);
    }

    /**
     * 修改基本配置
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward modifyCfg(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");

        RobotCFGOBJ bean = robotManageService.getRobotCFGOBJ(Integer
                .parseInt(id));

        boolean ifupdate = true;
        if (null == bean) {
            ifupdate = false;
            bean = new RobotCFGOBJ();
            bean.setIdRobot(Integer.parseInt(id));
        }

        if (!StringUtils.isEmpty(request.getParameter("logAfterSec")))
            bean.setLogAfterSec(Integer.valueOf(request
                    .getParameter("logAfterSec")));
        if (!StringUtils.isEmpty(request.getParameter("logBeforeSec")))
            bean.setLogBeforeSec(Integer.valueOf(request
                    .getParameter("logBeforeSec")));
        if (!StringUtils.isEmpty(request.getParameter("lostRateBaseUnit")))
            bean.setLostRateBaseUnit(Integer.valueOf(request
                    .getParameter("lostRateBaseUnit")));
        if (!StringUtils.isEmpty(request.getParameter("qualityBaseUnit")))
            bean.setQualityBaseUnit(Integer.valueOf(request
                    .getParameter("qualityBaseUnit")));
        if (!StringUtils.isEmpty(request.getParameter("recyleLogClearCycle")))
            bean.setRecyleLogClearCycle(Integer.valueOf(request
                    .getParameter("recyleLogClearCycle")));
        if (!StringUtils.isEmpty(request.getParameter("unuserLogClearCycle")))
            bean.setUnuserLogClearCycle(Integer.valueOf(request
                    .getParameter("unuserLogClearCycle")));

        request.setAttribute(NeConstant.REQUEST_ATTR_ROBOT, bean);
        LoggerUtils.logActionInfo("Modify the RobotCfgManage - robotId: " + id,
                logger, request);

        // 判断是修改还是更新.
        if (ifupdate) {
            robotManageService.modify(bean);
        } else {
            robotManageService.add(bean);
        }

        return mapping.findForward(GlobalConstant.FORWARD_SUCCESS);
    }

    /**
     * 修改文件配置
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward modifyFileInfo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");

        RobotFileInfoOBJ bean = robotManageService.getRobotFileInfoOBJ(Integer
                .parseInt(id));

        boolean ifupdate = true;
        if (null == bean) {
            ifupdate = false;
            bean = new RobotFileInfoOBJ();
            bean.setIdRobot(Integer.parseInt(id));
        }

        bean.setPcapFileDir(request.getParameter("pcapFileDir"));
        bean.setPcapUrlDir(request.getParameter("pcapUrlDir"));
        bean.setVideoFileDir(request.getParameter("videoFileDir"));
        bean.setVideoPicUrlDir(request.getParameter("videoPicUrlDir"));
        bean.setVideoPicFileDir(request.getParameter("videoPicFileDir"));
        bean.setVideoUrl(request.getParameter("videoUrl"));
        bean.setVideoUrlDir(request.getParameter("videoUrlDir"));
        bean.setWebHost(request.getParameter("webHost"));
        request.setAttribute(NeConstant.REQUEST_ATTR_ROBOT, bean);

        LoggerUtils.logActionInfo("Modify the RobotFileInfoOBJ - robotId: "
                + id, logger, request);

        // 判断是修改还是更新.
        if (ifupdate) {
            robotManageService.modify(bean);
        } else {
            robotManageService.add(bean);
        }

        return mapping.findForward(GlobalConstant.FORWARD_SUCCESS);
    }

    /**
     * 修改网络配置
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward modifyNetInfo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");

        RobotNetInfoOBJ bean = robotManageService.getRobotNetInfoOBJ(Integer
                .parseInt(id));

        boolean ifupdate = true;
        if (null == bean) {
            ifupdate = false;
            bean = new RobotNetInfoOBJ();
            bean.setIdRobot(Integer.parseInt(id));
        }

        bean.setComPort(request.getParameter("comPort"));
        bean.setIpAddress(request.getParameter("ipAddress"));
        if (!StringUtils.isEmpty(request.getParameter("irAgentPort")))
            bean.setIrAgentPort(Integer.valueOf(request
                    .getParameter("irAgentPort")));
        if (!StringUtils.isEmpty(request.getParameter("irPort")))
            bean.setIrPort(Integer.valueOf(request.getParameter("irPort")));
        if (!StringUtils.isEmpty(request.getParameter("picAgentPort")))
            bean.setPicAgentPort(Integer.valueOf(request
                    .getParameter("picAgentPort")));
        if (!StringUtils.isEmpty(request.getParameter("qosAgentPort")))
            bean.setQosAgentPort(Integer.valueOf(request
                    .getParameter("qosAgentPort")));
        if (!StringUtils.isEmpty(request.getParameter("videoAgentPort")))
            bean.setVideoAgentPort(Integer.valueOf(request
                    .getParameter("videoAgentPort")));

        bean.setRobotDesc(request.getParameter("robotDesc"));
        request.setAttribute(NeConstant.REQUEST_ATTR_ROBOT, bean);

        LoggerUtils.logActionInfo(
                "Modify the RobotNetInfoOBJ - robotId: " + id, logger, request);

        // 判断是修改还是更新.
        if (ifupdate) {
            robotManageService.modify(bean);

        } else {
            robotManageService.add(bean);

        }

        return mapping.findForward(GlobalConstant.FORWARD_SUCCESS);
    }

    /**
     * 
     * @param pRobotManageService
     */
    public void setRobotManageService(RobotManageService pRobotManageService) {
        robotManageService = pRobotManageService;
    }

    public void setRegionManageService(RegionManageService pRegionManageService) {
        regionManageService = pRegionManageService;
    }

}
